<template>
  <div id="app">
        <div :class="(this.$route.path !== '/login' && this.$route.path !== '/home')?'header-other':'header'">
        <div class="left-button">
          <img src="./assets/header-logo.png" alt="">
          <div
            v-if="this.$route.path !== '/login' && this.$route.path !== '/home'&& this.$route.path !== '/meau'"
            class="meau-button"
            @click="backMeau"
            >
            <img src="./assets/back.png" alt="">
            返回菜单</div
          >
        </div>
        <div class="right-button">
          <div @click="init" class="connect"><span>连接设备</span></div>
          <div class="connect-status" >
            <img src="assets/connect-status.png" alt="">
            连接状态:
            <span v-if="connect_state">已连接</span>
            <span v-else>未连接</span>
          </div>
        </div>

        </div>
        <div class="main">
            <router-view />
        </div>
  </div>
</template>

<style lang='less' scoped>
.connect:hover{
  cursor: pointer;
}
.connect{
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 30px;
    width: 168px;
    height: 68px;
    font-family: PingFangSC-Medium, PingFang SC;
    background-color: rgba(255, 255, 255, 0.2);
    border-radius: 4px;
    span{
      font-size: 32px;
      color: #FFFFFF;
    }
}
.connect-status {
  margin-right: 40px;
  height: 45px;
  font-size: 32px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #FFFFFF;
  line-height: 45px;
}
.meau-button:hover{
  cursor: pointer;
}
.meau-button{
  margin-left: 70px;
  display: flex;
  padding: 0 20px;
  align-items: center;
  justify-content: space-between;
  width: 165px;
  height: 68px;
  font-size: 32px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #FFFFFF;
  line-height: 45px;
  border-radius: 4px;
  background-color: rgba(255, 255, 255, 0.2);
}

.header{
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: absolute;
  width: 100%;
  height: 10vh;
  background-color:rgba(255,255,255,0.1);
}
.header-other{
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: absolute;
  width: 100%;
  height: 10vh;
  background: linear-gradient(170deg, #37BE98 0%, #0F7DDF 100%);
}
.main{
}

#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  height: 100vh;
}

.left-button{
  display: flex;
  align-items: center;
  padding-left: 100px;
}
.right-button{
  display: flex;
  align-items: center;
  padding-left: 100px;
}
</style>

<script>
import EventBus from "./utils/eventbus.js";
import { mapState } from "vuex";
export default {
  data() {
    return {
      connect_state: false,
      url: "ws://127.0.0.1:3000",
    };
  },
  methods: {
    backMeau() {
      if (this.$route.path === "/meau") {
        return;
      }
      this.$router.push("/meau");
    },
    init() {
      if (typeof WebSocket === "undefined") {
        alert("您的浏览器不支持socket");
      } else {
        // 实例化socket
        this.socket = new WebSocket(this.url);
        // 监听socket连接
        this.socket.onopen = this.open;
        // 监听socket错误信息
        this.socket.onerror = this.error;
        // 监听socket消息
        this.socket.onmessage = this.getMessage;
        // 监听socket关闭连接
        this.socket.onclose = this.close;

        window.socket = this.socket;
      }
    },
    // 以下均为监听函数
    open: function () {
      this.connect_state = true;
      this.socket.send("admin:123456");
    },
    error: function () {
      this.$message.error("找不到设备或者连接失败");
    },
    getMessage: function (msg) {
      msg = msg.data;
      if (msg.indexOf("sorry") !== -1) {
        this.$message({
          message: "socket认证失败",
          type: "error",
        });
      } else if (msg.indexOf("congratulation") !== -1) {
        this.$message({
          message: "设备连接成功",
          type: "success",
        });
      } else {
        if (msg.indexOf("recv") !== -1) {
          this.$message({
            message: "指令已下达",
            type: "success",
          });
        } else {
          let msgArr = msg.split(":");
          if (msgArr[0] === "flow") {
            let flowData = msgArr[1].split(",");
            EventBus.$emit("flow", flowData);
          } else if (msgArr[0] === "result") {
            let resultData = msgArr[1].split(",");
            // localStorage.setItem('result',)
            EventBus.$emit("result", resultData);
          }
        }
      }
    },
    close: function () {
      this.$message({
        message: "socket已经关闭",
        type: "error",
      });
      this.connect_state = false;
      window.socket = undefined;
    },
  },
  mounted() {
    this.init();
  },
};
</script>
 